package com.jt.service;

import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import com.jt.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceIml implements UserService{
    @Autowired
    private UserMapper userMapper;
    @Override
    public String login(User user) {
        //将密码加密处理
        String password = user.getPassword();
        //md5加密算法，进行加密
        String md5Pass = DigestUtils.md5DigestAsHex(password.getBytes());
         user.setPassword(md5Pass);
         User userDB = userMapper.findUserByUP(user);
         if (userDB == null){
             return null;
         }
         String uuid = UUID.randomUUID().toString().replace("-", "");

        return uuid;
    }

    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    @Override
    public PageResult getUserList(PageResult pageResult) {
        long total = userMapper.getTal(pageResult.getQuery());
        int size = pageResult.getPageSize();
        int start = (pageResult.getPageNum()-1) * size;
        String query =pageResult.getQuery();
        List<User> rows = userMapper.findUserListByPage(start,size,query);
        return pageResult.setRows(rows).setTotal(total);
    }
@Transactional
    @Override
    public void updateStatus(User user) {
        user.setUpdated(new Date());
        userMapper.updateStatus(user);
    }
@Transactional
    @Override
    public void addUser(User user) {
        Date date = new Date();
        String md5Pass = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        user.setPassword(md5Pass).setStatus(true).setCreated(date).setUpdated(date);

        userMapper.addUser(user);
    }

    @Override
    public User findUserById(Integer id) {

        return userMapper.findUserById(id);
    }
@Transactional
    @Override
    public void updateUser(User user) {
         userMapper.updateUser(user);
    }
@Transactional
    @Override
    public void deleteUser(Integer id) {
        userMapper.deleteUser(id);

    }


}
